package com.dating.activity;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.dating.entities.Her;
import com.dating.entities.RowChat;
import com.dating.pref.Function;
import com.dating.view.ChatAdapter;

public class Chat extends Activity{
	Handler mHandler;
	ListView mList;
	EditText mInput;
	Button mSend;
	
	int userID=-1;
	Her mHer;
	String[] content={"android","iphone","samsung","nokia","microsoft","motorola","HTC","Blackbery"};
	ArrayList<RowChat> array=new ArrayList<RowChat>();
	ChatAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.chat);
		mHandler=new Handler();
		// Initial data
		mHer=new Her(0, "0","0","0","0","0","http://img.2sao.vietnamnet.vn/2010/07/20/15/21/55207476tuannvChung20Han20Dong11.jpg","0","0","0","0","0","0","0","0","0");
		Bundle bundle = this.getIntent().getExtras();
		
		if (bundle != null && bundle.containsKey("user_id")) {
			try{
			// get avatar
			userID = Integer.valueOf(bundle.getString("user_id"));
			if(userID>-1){
				mHer._id_username=userID+"";
			}
			Toast.makeText(this, userID+"", Toast.LENGTH_SHORT).show();
			}catch(NumberFormatException e){}
		} else {
			// default
			Toast.makeText(this, "can not get id of user", Toast.LENGTH_SHORT).show();
			finish();
		}
		
		findById();
		
		for(int i=0;i<9;++i){
			String tag="";
			if(i%2==0)
				tag="her";
			else
				tag="me";
			
			RowChat _i=new RowChat(tag,"id:"+i, "", "12-23-2012", "item:"+i+"-i love you\n em yeu anh\n nen dau da roi. vi em da chot yeu ai em mai vo tinh khong nhan ra", null);
			array.add(_i);
		}
		
		adapter=new ChatAdapter(this, mHandler,mHer, array);
		// update avatar
		adapter.setAvatar(mHer._bmp);
		mList.setAdapter(adapter);
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				if(mHer._bmp==null){
					// avatar (new thread)
					try {
						URL url = new URL(mHer._link_image);
						if(url.toString().contains("http")){
							mHer._bmp=Function.getBitmapFromUrl(url);
							adapter.setAvatar(mHer._bmp);
						}
						return;
					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (Exception e){
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
	private void findById(){
		mList=(ListView)findViewById(R.id.list_chat);
		
	}
}
